from question_classifier_ekg import *

class QuestParser:
    '''构建实体节点'''

    def build_entitydict(self, args):
        entity_dict = {}
        for arg, types in args.items():
            for type in types:
                if type not in entity_dict:
                    entity_dict[type] = [arg]
                else:
                    entity_dict[type].append(arg)

        return entity_dict

    '''问题解析主函数'''

    def parser_main(self, res_classify):
        args = res_classify['args']
        entity_dict = self.build_entitydict(args)
        question_types = res_classify['question_types']
        sqls = []
        for question_type in question_types:
            sql_ = {'question_type': question_type}
            sql = []
            # 问题类型1：商品品牌
            if question_type == 'goods_brand':
                sql = self.sql_transfer(question_type, entity_dict.get('goods'))
            # 问题类型2：已知品牌找商品
            elif question_type == 'brand_goods':
                sql = self.sql_transfer(question_type, entity_dict.get('brand'))
            # 问题类型3：根据分类找商品
            elif question_type == 'category_goods':
                sql = self.sql_transfer(question_type, entity_dict.get('category'))
            # 问题类型4：根据主题找商品
            elif question_type == 'theme_goods':
                sql = self.sql_transfer(question_type, entity_dict.get('theme'))
            # 问题类型5：根据场景找商品
            elif question_type == 'scene_goods':
                sql = self.sql_transfer(question_type, entity_dict.get('scene'))
            # 问题类型6：根据人群找商品
            elif question_type == 'crowd_goods':
                sql = self.sql_transfer(question_type, entity_dict.get('crowd'))

            if sql:
                sql_['sql'] = sql
                sqls.append(sql_)
        return sqls

    '''针对不同的问题，分开进行处理'''

    def sql_transfer(self, question_type, entities):
        if not entities:
            return []
        # 限制输出最大项数
        num_limit = 10
        # 查询语句
        sql = []

        # 查询商品品牌
        if question_type == 'goods_brand':
            sql = ["MATCH (n:goods)-[:brand_to]-(m:brand) where n.name = '{0}' return m.name limit {1}".format(i, num_limit) for i in entities]
        # 查询特定人群的商品
        elif question_type == 'crowd_goods':
            sql = ["MATCH (m:goods)-[:crowd_to]-(n:crowd) where n.name = '{0}' return m.name limit {1}".format(i, num_limit) for i in entities]

        # 允许模糊查询，即输入关键字在同义词中
        # 查询特定品牌的商品
        elif question_type == 'brand_goods':
            sql = ["MATCH (m:goods)-[:brand_to]-(n:brand) where n.name = '{0}' or '{0}' in n.synonym return m.name limit {1}".format(i, num_limit) for i in entities]
        # 查询特定分类的商品
        elif question_type == 'category_goods':
            sql = ["MATCH (m:goods)-[:belong_to]-(n:category) where '{0}' in n.name  or '{0}' in n.synonym return m.name limit {1}".format(i, num_limit) for i in entities]
        # 查询特定主题的商品
        elif question_type == 'theme_goods':
            sql = ["MATCH (m:goods)-[:aim_to]-(n:theme) where '{0}' in n.name or '{0}' in n.synonym return m.name limit {1}".format(i, num_limit) for i in entities]
        # 查询特定场景的商品
        elif question_type == 'scene_goods':
            sql = ["MATCH (m:goods)-[:apply_to]-(n:scene) where '{0}' in n.name or '{0}' in n.synonym return m.name limit {1}".format(i, num_limit) for i in entities]

        return sql


'''测试'''

if __name__ == '__main__':
    classifier = QuestClassifier()
    parser = QuestParser()
    res_classify = classifier.classify(input("智能搜索："))
    print(res_classify)
    res_sql = parser.parser_main(res_classify)
    print(res_sql)

